package repository

import (
	"context"
	"database/sql"

	"gitee.com/hp-huiw/family-expense-api/internal/db"
	dbmodel "gitee.com/hp-huiw/family-expense-api/internal/db/model"
	jet "github.com/go-jet/jet/v2/mysql"
)

type IncomeExpenseDetailBaseTypeRepository interface {
	Exists(ctx context.Context) (bool, error)
	QueryAll(ctx context.Context) ([]dbmodel.IncomeExpenseDetailBaseType, error)
	BatchInsert(ctx context.Context, models []dbmodel.IncomeExpenseDetailBaseType) error
}

func NewIncomeExpenseDetailBaseTypeRepository(db *sql.DB) IncomeExpenseDetailBaseTypeRepository {
	return &incomeExpenseDetailBaseTypeRepository{db: db}
}

// Exists checks if any existing data.
func (r *incomeExpenseDetailBaseTypeRepository) Exists(ctx context.Context) (bool, error) {
	return db.Exists(ctx, r.db, tbDetailBaseType, nil)
}

// QueryAll queries all IncomeExpenseDetailBaseTypes.
func (r *incomeExpenseDetailBaseTypeRepository) QueryAll(ctx context.Context) ([]dbmodel.IncomeExpenseDetailBaseType, error) {
	query := []dbmodel.IncomeExpenseDetailBaseType{}

	stmt := jet.
		SELECT(
			tbDetailBaseType.AllColumns,
		).
		FROM(
			tbDetailBaseType,
		)

	if err := stmt.QueryContext(ctx, r.db, &query); err != nil {
		return nil, err
	}

	return query, nil
}

// BatchInsert inserts multiple incomeExpenseDetailBaseTypes.
func (r *incomeExpenseDetailBaseTypeRepository) BatchInsert(ctx context.Context, models []dbmodel.IncomeExpenseDetailBaseType) error {
	stmt := tbDetailBaseType.
		INSERT().
		MODELS(models)

	_, err := stmt.ExecContext(ctx, r.db)

	return err
}

type incomeExpenseDetailBaseTypeRepository struct {
	db *sql.DB
}
